# import cv2
# import numpy as np
#
#
# # 初始化HSV范围
# h_min, h_max = 0, 180
# s_min, s_max = 0, 255
# v_min, v_max = 0, 255
#
# # 创建滑动条回调函数
# def nothing(x):
#     pass
#
# # 读取图片
# image = cv2.imread("../images/xhd.jpg")
#
# # 转换为HSV颜色空间
# hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)
#
# # 创建窗口
# cv2.namedWindow('Mask')
#
# # 创建滑动条
# cv2.createTrackbar('HMin', 'Mask', h_min, 180, nothing)
# cv2.createTrackbar('HMax', 'Mask', h_max, 180, nothing)
# cv2.createTrackbar('SMin', 'Mask', s_min, 255, nothing)
# cv2.createTrackbar('SMax', 'Mask', s_max, 255, nothing)
# cv2.createTrackbar('VMin', 'Mask', v_min, 255, nothing)
# cv2.createTrackbar('VMax', 'Mask', v_max, 255, nothing)
#
# while True:
#     # 获取滑动条的当前值
#     h_min = cv2.getTrackbarPos('HMin', 'Mask')
#     h_max = cv2.getTrackbarPos('HMax', 'Mask')
#     s_min = cv2.getTrackbarPos('SMin', 'Mask')
#     s_max = cv2.getTrackbarPos('SMax', 'Mask')
#     v_min = cv2.getTrackbarPos('VMin', 'Mask')
#     v_max = cv2.getTrackbarPos('VMax', 'Mask')
#
#     # 定义HSV范围
#     lower_bound = np.array([h_min, s_min, v_min])
#     upper_bound = np.array([h_max, s_max, v_max])
#
#     # 创建掩码
#     mask = cv2.inRange(hsv_image, lower_bound, upper_bound)
#
#     # 显示掩码
#     cv2.imshow('Mask', mask)
#
#     # 按ESC键退出
#     if cv2.waitKey(1) & 0xFF == 27:
#         break
#
# cv2.destroyAllWindows()

import cv2
import numpy as np

# 初始化HSV范围
h_min, h_max = 0, 180
s_min, s_max = 0, 255
v_min, v_max = 0, 255

# 创建滑动条回调函数
def nothing(x):
    pass

# 读取图片
image = cv2.imread("../images/xhd.jpg")

# 转换为HSV颜色空间
hsv_image = cv2.cvtColor(image, cv2.COLOR_BGR2HSV)

# 创建窗口
cv2.namedWindow('Mask')

# 创建滑动条
cv2.createTrackbar('H Min', 'Mask', h_min, 180, nothing)
cv2.createTrackbar('H Max', 'Mask', h_max, 180, nothing)
cv2.createTrackbar('S Min', 'Mask', s_min, 255, nothing)
cv2.createTrackbar('S Max', 'Mask', s_max, 255, nothing)
cv2.createTrackbar('V Min', 'Mask', v_min, 255, nothing)
cv2.createTrackbar('V Max', 'Mask', v_max, 255, nothing)

while True:
    # 获取滑动条的当前值
    h_min = cv2.getTrackbarPos('H Min', 'Mask')
    h_max = cv2.getTrackbarPos('H Max', 'Mask')
    s_min = cv2.getTrackbarPos('S Min', 'Mask')
    s_max = cv2.getTrackbarPos('S Max', 'Mask')
    v_min = cv2.getTrackbarPos('V Min', 'Mask')
    v_max = cv2.getTrackbarPos('V Max', 'Mask')

    # 定义HSV范围
    lower_bound = np.array([h_min, s_min, v_min])
    upper_bound = np.array([h_max, s_max, v_max])
    # 创建掩码
    mask = cv2.inRange(hsv_image, lower_bound, upper_bound)
    # 显示掩码
    cv2.imshow('Mask', mask)
    # 按ESC键退出
    if cv2.waitKey(1) & 0xFF == 27:
        break
cv2.destroyAllWindows()